<template>
    <div class="content">
        <el-row :gutter="20">
            <el-col :span="12">
                <el-button type="primary" class="ml10" :icon="Search" @click="onEdit(0)">添加</el-button>
            </el-col>
            <el-col :span="12" class="text-right">
                <el-input v-model="form.search" style="width: 240px" placeholder="请输入关键词" />
                <el-button type="primary" class="ml10" :icon="Search" @click="onSearch">搜索</el-button>
                <el-button type="primary" class="ml10" :icon="RefreshRight" @click="onSearch('Refresh')" />
            </el-col>
        </el-row>
        
        <el-table class="mt20" size="small" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">																		
            <el-table-column type="index" :index="indexMethod" label="序号" width="60" />
            <el-table-column label="项目名称">
                <template #default="scope">
                    <el-text>{{scope.row.project.title}}</el-text>
                </template>
            </el-table-column>
            <el-table-column prop="amount" label="合同金额" width="90"></el-table-column>
            <el-table-column prop="gap" label="资金缺口" width="90"></el-table-column>
            <el-table-column prop="usage_days" label="使用（天）" width="90"></el-table-column>
            <el-table-column prop="usage_start_date" label="使用时间" ></el-table-column>
            <el-table-column prop="repayment_date" label="还款时间" ></el-table-column>
            <el-table-column prop="interest" label="总利息" width="90"></el-table-column>
            <el-table-column prop="remarks" label="备注" width="90"></el-table-column>
            <el-table-column fixed="right" label="操作" width="80">
                <template #default="scope">
                    <el-link type="primary" @click="onEdit(scope.row.id)" >编辑</el-link>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt20">
            <el-pagination v-model:current-page="form.current" v-model:page-size="form.limit" :page-sizes="[10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="form.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
    </div>


    <el-dialog v-model="isEdit" title="申请操作" width="50%">
        <component :is="salaryForm" :data = editId @onSave="onSave" />
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref,onMounted } from 'vue'
import {  RefreshRight, Search } from '@element-plus/icons-vue'
import http from '../../../util/request';
import salaryForm from './components/salaryForm.vue';
import router from '../../../router';
const indexMethod = (index: number) => {
  return index+= 1
}
const isEdit = ref<boolean>(false)
const editId = ref<number>(0)
const form = ref<any>({search:'',issue_date:'',page:0,current:0,total:10,limit:20,loading:false,parentBorder:false})
interface User {
    id:number
    amount:number
    project_id: number
    gap:number
    usage_days:number
    usage_start_date:string
    repayment_date:string
    interest:number
    remarks:string
}

const tableData = ref<User[]>([])

const onGetData = () => {
    
    form.value.loading = true
    http.post('/capitalize',form.value).then((response:any)=>{
        form.value.loading = false
        tableData.value = response.data.data
        form.value.total = response.data.total
    })
}


const onEdit = (id:number) => {
    router.push({ name: 'financeCapitalizeEdit',params: { id: id }})
}




const onSearch = (type = '') => {
    if(type == 'Refresh'){
        form.value.search = ''
        onGetData()
        return
    }
    if(!form.value.search){
        // ElMessage.error('请输入搜索内容')
    }
    form.value.page = 1
    onGetData()
}

const handleSizeChange = (val: number) => {
    form.value.limit = val
    onGetData()
}
const handleCurrentChange = (val: number) => {
    form.value.page = val
    onGetData()
}
onMounted(()=>{
    onGetData()
})

const onSave = () => {
    isEdit.value = false
    onGetData()
}

</script>

<style scoped>

</style>